package de.hska.pwmanager.controller;

import static de.hska.pwmanager.validation.ValidationUtils.*;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import de.hska.pwmanager.controller.errors.ValidationError;
import de.hska.pwmanager.controller.errors.ValidationException;
import de.hska.pwmanager.controller.interfaces.ICRUD;
import de.hska.pwmanager.persistence.model.PasswordCategory;
import de.hska.pwmanager.persistence.model.PasswordData;
import de.hska.pwmanager.persistence.model.dao.PasswordCategoryDao;
import de.hska.pwmanager.persistence.model.dao.PasswordDataDao;

@Controller

public class PasswordCategoryController extends AbstractController implements IPasswordCategoryController {
	@Autowired
	private PasswordCategoryDao passwordCategoryDao;
	@Autowired
	private PasswordController passwordController;
	@Autowired
	private PasswordDataDao passwordDataDao;

	public @ResponseBody
	List<PasswordCategory> readAll() {
		return this.passwordCategoryDao.readAll();
	}

	public @ResponseBody
	void createOrUpdate(@RequestBody PasswordCategory toCreateOrUpdate)
			throws ValidationException {
		this.validate(toCreateOrUpdate);
		this.passwordCategoryDao.save(toCreateOrUpdate);
	}

	public @ResponseBody
	void delete(@PathVariable(value = "id") Long primaryKey) throws ValidationException {
		PasswordCategory category = this.passwordCategoryDao
				.readByPrimaryKey(primaryKey);
		List<PasswordData> passwords = this.passwordDataDao
				.findByPasswordCategory(category);
		this.passwordController.delete(passwords);
		this.passwordCategoryDao.delete(category);
	}

	public void validate(PasswordCategory toValidate)
			throws ValidationException {
		ValidationException exception = new ValidationException();
		if (isNullOrEmpty(toValidate.getPasswordCategoryName())) {
			exception.addError(new ValidationError("passwordCategoryName",
					"The field passwordCategoryName cannot be empty"));
		} else {
			if(toValidate.getId()==null&&this.passwordCategoryDao.findByPasswordCategoryName(toValidate.getPasswordCategoryName()).size()>0) {
				exception.addError(new ValidationError("passwordCategoryName",
				"A category with that name already exists"));
				// here comes the tricky part...
			} else {
				if(this.passwordCategoryDao.findByPasswordCategoryNameAndIdNot(toValidate.getPasswordCategoryName(), toValidate.getId()).size()>0) {
					exception.addError(new ValidationError("passwordCategoryName",
					"A category with that name already exists"));	
				}
			}
		}
		
		exception.throwMe();
	}

	public @ResponseBody
	PasswordCategory readByPK(@PathVariable(value = "id") Long pk) {
		return this.passwordCategoryDao.readByPrimaryKey(pk);
	}

}
